<?php
/**
 * Created by PhpStorm.
 * User: Korbin
 * Date: 2018/11/26
 * Time: 11:43
 */

namespace app\script\command;

use app\common\model\NewsContent;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;

class DelNewsContentReplace extends Base
{

    /**
     * 设置执行名称及配置传入参数、脚本描述
     */
    protected function configure()
    {
        //设置执行名称
        $this->setName('delNewsContent')
            ->setDescription('Run all the time');
    }

    /*********************公用验证部分************************************/
    /*
     * 执行脚本任务
     *
     * @param Input $input
     * @param Output $output
     */
    protected function execute(Input $input, Output $output)
    {
        $output->writeln('开始执行：' . date('Y-m-d H:i:s'));

        $newContent=new NewsContent();
        $sql = <<<SQL
SELECT
	a.id
FROM cap_news_content a
LEFT JOIN cap_news b on a.news_id = b.id
WHERE
	b.id IS NULL
limit 100000
SQL;

        $res = $newContent->query($sql);
        $i=0;
        foreach ($res as $key=>$arr){
            $newContent->where(['id'=>$arr['id']])->delete();
            $output->writeln('删除成功：ID【'.$arr['id'].'】' . date('Y-m-d H:i:s'));
            $i++;
        }
        $output->writeln('执行完毕：总共删除【'.$i.'】条数据' . date('Y-m-d H:i:s'));

    }
}